package com.bawei.bydayzg_two

import android.R.attr
import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup.LayoutParams
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.mvvm.BaseActivity
import com.bawei.bydayzg_two.Utils.GildUtils
import com.bawei.bydayzg_two.adapter.Pladapter
import com.bawei.bydayzg_two.databinding.ActivityDetail2Binding
import com.bawei.bydayzg_two.viewModel.DetailViewModel
import com.blankj.utilcode.util.ToastUtils
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeEncoder
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.editorpage.ShareActivity
import com.umeng.socialize.media.UMImage


@Route(path = "/app/DetailActivity2"  )
class DetailActivity2 : BaseActivity<ActivityDetail2Binding,DetailViewModel>() {
    var list:ArrayList<String> = arrayListOf()
    lateinit var pladapter: Pladapter
    //
    var arrtId = ""//默认选中属性的id，只有数据加载完成后才能赋值
    //
    @Autowired
    @JvmField
    var goodsId = 1
    //
    override fun getLoutyId(): Int {
        return R.layout.activity_detail2
    }

    override fun getViewModel(): Class<DetailViewModel> {
        return DetailViewModel::class.java
    }

    override fun initView() {

        //
        ARouter.getInstance().inject(this)
        //
        pladapter = Pladapter()
        binding.rvComment.adapter = pladapter
        binding.rvComment.layoutManager = LinearLayoutManager(this)
        //
        viewModel.detail(goodsId,"normal")

        //点击添加购物车
        binding.tvAddCart.setOnClickListener {
            viewModel.add(1,arrtId,goodsId)
        }

        //去购物车
        binding.goCart.setOnClickListener {
            ARouter.getInstance().build("/app/ShopActivity").withInt("index",2).navigation()
        }

        //点击分享弹出自定义对话框，点在对应的二维码进行分享
        binding.share.setOnClickListener {
            val inflate = LayoutInflater.from(this).inflate(R.layout.item_share, null)
            //生成二维码
            val bitmap =
                BarcodeEncoder().encodeBitmap(goodsId.toString(), BarcodeFormat.QR_CODE, 200, 200)
            //显示二维码
            inflate.findViewById<ImageView>(R.id.iv).setImageBitmap(bitmap)
            //点击弹出对话框
            AlertDialog.Builder(this)
                .setTitle("分享")
                .setView(inflate)
                .setNeutralButton("取消",null)
                .setPositiveButton("分享"){a,b->
                    val image: UMImage = UMImage(this@DetailActivity2, bitmap) //bitmap文件
                    //
                    ShareAction(this@DetailActivity2).withText("hello").withMedia(image)
                        .setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN)
                        .setCallback(object :UMShareListener{
                            override fun onStart(p0: SHARE_MEDIA?) {

                            }

                            override fun onResult(p0: SHARE_MEDIA?) {

                            }

                            override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
                                Log.e("TAG", "分享: ${p1?.message}" )
                            }

                            override fun onCancel(p0: SHARE_MEDIA?) {

                            }
                        })
                        .open();
                }.show()
        }

        //获取评论
        viewModel.pl(goodsId,10,1,"0")

        //用户评价
        binding.tvComment.setOnClickListener {
            //
            ARouter.getInstance().build("/app/MessageActivity").navigation()
        }
    }

    override fun initDate() {

        //
        viewModel.detailSuccess.observe(this){
            if (it.code == 200){
                arrtId = it.data.productValue.values.toList()[0].id.toString()
                //
                binding.info = it.data.productInfo
                //显示轮播图
                list.add(it.data.productInfo.image)
                list.add(it.data.productInfo.image)
                list.add(it.data.productInfo.image)
                //
//                binding.banner.setImages(list)
//                    .setImageLoader(object :ImageLoader(){
//                        override fun displayImage(
//                            context: Context?,
//                            path: Any?,
//                            imageView: ImageView?
//                        ) {
//                            if (context != null && path != null && imageView != null){
//                                Glide.with(context).load(path).into(imageView)
//                            }
//                        }
//                    }).setDelayTime(2000)
//                    .setBannerStyle(BannerConfig.NUM_INDICATOR)
//                    .start()
                binding.imgs = list
                //选中默认的规格名称
                binding.tvSku.text = "已选择${it.data.productValue.keys.toList()[0]}"

            }
            //
            ToastUtils.showLong(it.message)
        }
        //
        viewModel.detailFail.observe(this){
            ToastUtils.showLong(it)
        }

        //
        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)
            //
            if (it.code == 200){
                //添加成功狗创建图片
                val imageView = ImageView(this)
                val layoutParams = LayoutParams(200, 200)
                imageView.layoutParams = layoutParams
                //添加图片
                GildUtils.loadImg(this,list[0],imageView)
                //
                binding.main.addView(imageView)
                //屏幕宽度
                val width    = windowManager.defaultDisplay.width.toFloat()
                val height    = windowManager.defaultDisplay.height.toFloat()
                //设置动画效果
                val transX   =
                    ObjectAnimator.ofFloat(imageView, "translationX", 0f, width, width / 3)
                val transY   = ObjectAnimator.ofFloat(imageView, "translationY", 0f, height)
                val set = AnimatorSet()
                set.duration = 3000
                set.play(transX).with(transY)
                set.start()
                //
                set.addListener(object :Animator.AnimatorListener{
                    override fun onAnimationStart(animation: Animator) {

                    }

                    override fun onAnimationEnd(animation: Animator) {
                        //移除图片
                        binding.main.removeView(imageView)
                    }

                    override fun onAnimationCancel(animation: Animator) {

                    }

                    override fun onAnimationRepeat(animation: Animator) {

                    }
                })
            }
        }
        //
        viewModel.addFail.observe(this){
            ToastUtils.showLong( it)
        }

        //
        viewModel.plSuccess.observe(this){
            if (it.code == 200){
                pladapter.data.clear()
                pladapter.data.addAll(it.data.list)
                pladapter.notifyDataSetChanged()
            }
            //
            ToastUtils.showLong(it.message)
        }

    }

}